//package net.lcqf.tcp.config;
//
//import cn.hutool.core.util.StrUtil;
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.DeserializationFeature;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
//import org.springframework.cache.annotation.EnableCaching;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.connection.RedisNode;
//import org.springframework.data.redis.connection.RedisSentinelConfiguration;
//import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
//import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//import redis.clients.jedis.JedisPoolConfig;
//
//import java.time.Duration;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * Redis 配置
// * +----------------------------------------------------------------------
// * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// * +----------------------------------------------------------------------
// * | Copyright (c) 2018-2024 聊来All rights reserved.
// * +----------------------------------------------------------------------
// * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
// * +----------------------------------------------------------------------
// * | @Author: 贯彻己道的云 <18970881148@qq.com>
// * +----------------------------------------------------------------------
// * | DateTime: 2024/6/2 14:08
// * +----------------------------------------------------------------------
// */
//
//@Configuration
//@EnableCaching
//public class RedisConfig {
//
//    @Value("${spring.redis.timeout}")
//    private String timeout;
//
//    @Value("${spring.redis.jedis.pool.max-active}")
//    private int maxTotal;
//
//    @Value("${spring.redis.jedis.pool.max-idle}")
//    private int maxIdle;
//
//    @Value("${spring.redis.jedis.pool.min-idle}")
//    private int minIdle;
//
//    @Value("${spring.redis.jedis.pool.max-wait}")
//    private int maxWaitMillis;
//
//    @Value("${spring.redis.jedis.pool.time-between-eviction-runs}")
//    private String timeBetweenEvictionRunsMillis;
//
//    private final RedisProperties redisProperties;
//
//    public RedisConfig(RedisProperties redisProperties) {
//        this.redisProperties = redisProperties;
//    }
//
//    @Bean
//    public JedisPoolConfig jedisPoolConfig() {
//        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//        jedisPoolConfig.setMaxIdle(maxIdle);
//        jedisPoolConfig.setMaxTotal(maxTotal);
//        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
//        jedisPoolConfig.setMinIdle(minIdle);
//        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(Integer.parseInt(timeBetweenEvictionRunsMillis));
//        return jedisPoolConfig;
//    }
//
//    @Bean
//    public RedisConnectionFactory taskConnectionFactory() {
//        //获得默认的连接池构造
//        //这里需要注意的是，RedisConnectionFactory对于Standalone模式的没有（RedisStandaloneConfiguration，jedisPoolConfig）的构造函数，对此
//        //我们用JedisClientConfiguration接口的builder方法实例化一个构造器，还得类型转换
//        JedisClientConfiguration.DefaultJedisClientConfigurationBuilder jpConfigBuilder = (JedisClientConfiguration.DefaultJedisClientConfigurationBuilder) JedisClientConfiguration.builder();
//        //修改我们的连接池配置
//        jpConfigBuilder.usePooling();
//        jpConfigBuilder.poolConfig(jedisPoolConfig());
//
//        jpConfigBuilder.readTimeout(Duration.ofMillis(Integer.parseInt(timeout)));
//        jpConfigBuilder.connectTimeout(Duration.ofMillis(Integer.parseInt(timeout)));
//        //通过构造器来构造jedis客户端配置
//        JedisClientConfiguration jedisClientConfiguration = jpConfigBuilder.build();
//
//        if (redisProperties.getSentinel() != null) {
//            RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
//            configuration.setDatabase(redisProperties.getDatabase());
//            configuration.setMaster(redisProperties.getSentinel().getMaster());
//            configuration.setPassword(redisProperties.getPassword());
//            configuration.setSentinels(createSentinels(redisProperties.getSentinel().getNodes()));
//
//            return new JedisConnectionFactory(configuration, jedisClientConfiguration);
//        } else if (redisProperties.getCluster() != null) {
//            // 集群
//            System.out.println("暂时不写");
//            return null;
//        } else {
//            // 普通模式
//            RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
//            configuration.setPort(redisProperties.getPort());
//            configuration.setHostName(redisProperties.getHost());
//            configuration.setDatabase(redisProperties.getDatabase());
//            configuration.setPassword(redisProperties.getPassword());
//
//            return new JedisConnectionFactory(configuration, jedisClientConfiguration);
//        }
//    }
//
//    /**
//     * 创建哨兵集群节点
//     * @param sentinels 节点
//     * @return {@code List<RedisNode>}
//     */
//    private List<RedisNode> createSentinels(List<String> sentinels) {
//        List<RedisNode> nodes = new ArrayList<>();
//        for (String sentinel : sentinels) {
//            String[] value = StrUtil.split(sentinel, ":").toArray(new String[0]);
//            nodes.add(new RedisNode(value[0], Integer.parseInt(value[1])));
//        }
//
//        return nodes;
//    }
//
//
//    /**
//     * redisTemplate相关配置
//     * 解决 Spring Boot 使用 RedisTemplate 存储键值出现乱码\x00的情况
//     * @param redisConnectionFactory redis链接工厂
//     *
//     */
//    @Bean
//    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
//        //配置redisTemplate
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        // 使用StringRedisSerializer来序列化和反序列化redis的key值
//        StringRedisSerializer stringSerializer = new StringRedisSerializer();
//        // 用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
//        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = serializer();
//
//        // value、hash的value 采用 Jackson 序列化方式
//        redisTemplate.setKeySerializer(stringSerializer);
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setHashKeySerializer(stringSerializer);
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.afterPropertiesSet();
//
//        return redisTemplate;
//    }
//
//    /**
//     * 配置Jackson2JsonRedisSerializer序列化策略
//     */
//    private Jackson2JsonRedisSerializer<Object> serializer() {
//        //设置序列化
//        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        // 指定要序列化的域，field,get和set,以及修饰符范围，ANY是都有包括private和public
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        // 指定序列化输入的类型，类必须是非final修饰的，final修饰的类，比如String,Integer等会跑出异常
//        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
//        // 不存在的字段,不被序列化
//        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//        return jackson2JsonRedisSerializer;
//    }
//}
